package com.evidencer.java.utils;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * @Title: XmlUtils.java
 * @Package com.evidencer.java.utils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2014/01/13 13:34:38
 * @version V1.0
 */
public class XmlUtils {

	private static String docPath = "src\\exam.xml";

	/**
	 * 获得文档对象
	 * @Title: getDocument
	 * @Description: TODO
	 * @param @return
	 * @param @throws ParserConfigurationException
	 * @param @throws SAXException
	 * @param @throws IOException
	 * @return Document
	 * @throws
	 */
	public static Document getDocument() throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder dBuilder = dBuilderFactory.newDocumentBuilder();
		return dBuilder.parse(docPath);
	}

	/**
	 * 更新文档
	 * @Title: updateDocument
	 * @Description: TODO
	 * @param @param d
	 * @param @throws TransformerException
	 * @return void
	 * @throws
	 */
	public static void updateDocument(Document document)
			throws TransformerException {
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer trans = tf.newTransformer();
		trans.transform(new DOMSource(document),new StreamResult(docPath));
	}
}
